const goodsService = require('../../service/goodsService.js')
const indexServive = require('../../service/indexService.js')
const addressService = require('../../service/addressService.js')
var amapFile = require('../../lib/amap-wx.js');
const utils = require('../../utils/util.js');
// load promise 
require('../../utils/wx-pro.js');
var app = getApp()
Page({

  data: {
    formatted_address: "",
    selectShopInfo: '',
    IS_EXPRESS: false,
    selectAddressInfo: '',
    selectShopId: '',
    pageParam: {
      pageNo: 1,
      pageSize: 10
    },
    xcxGoodsList: [],
    reqDelay: false,
    isEmpty: true,
    totalPage: '',
    showToast: false,
    toastMsg: '',
    bannerList: [],
    topiclist:[],
    floorlist:[]
  },

  onLoad: function () {
    
  },

  //错误提示
  showToastFuc(msg){
    this.setData({
      showToast: true,
      toastMsg: msg
    })
    setTimeout(() => {
      this.setData({
        showToast: false,
        toastMsg: ''
      })
    }, 2000)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (wx.getStorageSync('selectShopInfo')) {
      this.setData({
        selectShopInfo: JSON.parse(wx.getStorageSync('selectShopInfo'))
      })
      if (wx.getStorageSync('IS_EXPRESS')) {
        this.setData({
          IS_EXPRESS: wx.getStorageSync('IS_EXPRESS')
        })
      }
      if (!this.data.selectShopId || this.data.selectShopId != this.data.selectShopInfo.id){
        console.log("切换了店铺" + this.data.selectShopInfo.id + this.data.selectShopInfo.name)
        this.getShopInfoByShopId()
        this.setData({
          selectShopId: this.data.selectShopInfo.id
        })
        this.gettopic()//获取专题列表
        this.getfloor()//获取楼层列表
        if (wx.getStorageSync('selectAddressInfo')) {
          this.setData({
            selectAddressInfo: JSON.parse(wx.getStorageSync('selectAddressInfo'))
          })
        } 
        this.setData({
          isEmpty: true,
          pageParam: {
            pageNo: 1,
            pageSize: 10
          },
        })
        this.getGoodsList()
        this.xcxListAdvert()
      }
    }else{
      wx.navigateTo({
        url: '../selectAddress/selectAddress'
      })
    }
  },

  xcxListAdvert(){
    var params = {
      shopId: this.data.selectShopId,
      position: 1
    }
    goodsService.xcxListAdvert(params).then(data => {
      if (data.code == 200) {
        this.setData({
          bannerList: data.response.list
        })
      } else {
        this.showToastFuc(data.message)
      }
    })
      .catch((e) => {
        console.log(e)
      })
  },

  bannerlink(event) {
    var bannerLinkData = event.currentTarget.dataset.link
    console.log(bannerLinkData)
    if (bannerLinkData.type == 2) {
      wx.navigateTo({
        url: '../goodsDetail/goodsDetail?goodId=' + bannerLinkData.objectId
      })
    } else if (bannerLinkData.type == 3) {
      app.globalData.cate_id = bannerLinkData.objectId
      wx.switchTab({
        url: '../classfy/classfy'
      })
    }
  },

  getGoodsList(){
    this.setData({
      reqDelay: true
    });
    var params = this.data.pageParam
    params.shopId = this.data.selectShopId
    goodsService.getGoodsByGoodsKind(params).then(data => {
      if (data.code == 200) {
        this.setData({
          totalPage: data.response.page.totalPage
        })
        var goodsList = []
        var List = data.response.xcxGoodsList
        var len = List.length
        for (var i = 0; i < len; i++) {
          if (List[i].stockNum > 0 && List[i].ptPrice) {
            goodsList.push(List[i])
          }
        }
        var totalList = {}
        //如果要绑定新加载的数据，那么需要同旧有的数据合并在一起
        if (!this.data.isEmpty) {
          totalList = this.data.xcxGoodsList.concat(goodsList);
        }
        else {
          totalList = goodsList;
          this.data.isEmpty = false;
        }
        this.setData({
          xcxGoodsList: totalList,
          reqDelay: false
        });
        wx.hideNavigationBarLoading();
      }else{
        this.showToastFuc(data.message)
      }
    })
    .catch(() => {
      console.log('error')
    })
  },

  locationlink(){
    wx.navigateTo({
      url: '../selectAddress/selectAddress'
    })
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      pageParam: {
        pageNo: 1,
        pageSize: 10
      },
      xcxGoodsList: [],
      isEmpty: true,
      bannerList: []
    })
    this.getGoodsList()
    this.xcxListAdvert()
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (!this.data.reqDelay) {
      if (this.data.pageParam.pageNo >= this.data.totalPage) {
        console.log("没有了")
      } else {
        console.log(2222222222)
        wx.showNavigationBarLoading()
        this.setData({
          pageParam: {
            pageNo: this.data.pageParam.pageNo + 1,
            pageSize: 10
          },
        })
        this.getGoodsList()
      }
    }
  },

  /**
   * 专题的函数方法
   */
  gettopic(){
    var _this=this
    var params = {
      shopId: this.data.selectShopId,
      position: 0
    }
    indexServive.gettopiclist(params).then(r=>{
      // console.log("indexServive",r)
      if(r.code==200){
        this.setData({
          topiclist: r.response.topicPages
        })
      }else{
        _this.showToastFuc(r.message)
      }
    })
  },
  /**
   * 楼层的函数方法
   */
  getfloor(){
    var _this = this
    var params = {
      shopId: this.data.selectShopId,
      position: 0
    }
    indexServive.getfloorlist(params).then(r=>{
      if(r.code==200){
        this.setData({
          floorlist:r.response.floors
        })
      }else{
        _this.showToastFuc(r.message)
      }
    })
  },
  getShopInfoByShopId() {//依据shopid获取店铺信息
  var _this=this
    var shopid = JSON.parse(wx.getStorageSync('selectShopInfo'))
    var params = {
      shopId: shopid.id
    }
    addressService.getShopById(params).then(data => {
      if (data.code == 200) {
        wx.setStorageSync('shopdetail', JSON.stringify(data.response.shop))
      } else {
        _this.showToastFuc(data.message)
      }
    })
      .catch(() => {
        console.log('error')
      })
  }
})